"use strict";

const await = require("await-stream-ready/lib/await");

const Controller = require("egg").Controller;

class LiveController extends Controller {
  async index() {
    const { ctx, app } = this;
    let tabs = [
      {
        name: "全部",
        url: "/admin/live",
        active: false,
      },
      {
        name: "直播中",
        url: "?status=1",
        status: 1,
        active: false,
      },
      {
        name: "未开播",
        url: "?status=0",
        status: 0,
        active: false,
      },
      {
        name: "直播结束",
        url: "?status=3",
        status: 3,
        active: false,
      },
    ];
    tabs = tabs.map((item) => {
      if (
        (!ctx.query.status &&
          ctx.query.status != 0 &&
          item.url === "/admin/live") ||
        item.status == ctx.query.status
      ) {
        item.active = true;
      }
      return item;
    });
    let where =
      !ctx.query.status && ctx.query.status != 0
        ? {}
        : { status: ctx.query.status };

    let data = await ctx.page("Live", where, {
      include: [
        {
          model: app.model.User,
          attributes: ["id", "username"],
        },
      ],
    });
    data = JSON.parse(JSON.stringify(data));
    await ctx.renderTemplate({
      title: "直播间列表",
      tempType: "table",
      table: {
        tabs,
        //表头
        columns: [
          {
            title: "直播间",
            fixed: "left",
            render(item) {
              let image = item.cover
                ? item.cover
                : "/public/assets/img/profiles/avatar-01.jpg";
              return `
                        <h2>
                            <a class="avatar avatar-sm mr-2"><img class="avatar-img rounded-circle bg-light" src="${image}"></a>
                            <a>
                                ${item.title}
                                <span>创建人： ${item.user.username}</span>
                            </a>
                        </h2>`;
            },
          },
          {
            title: "观看人数",
            fixed: "center",
            key: "look_count",
          },
          {
            title: "金币数",
            fixed: "center",
            key: "coin",
          },
          {
            title: "创建时间",
            key: "created_time",
            width: 180,
            fixed: "center",
          },
          {
            title: "操作",
            width: 200,
            fixed: "center",
            action: {
              delete: function (id) {
                return `/admin/order/delete/${id}`;
              },
            },
            render(item) {
              let close = "";
              if (item.status !== 3) {
                close = `<a @click="modal('/admin/live/close/${item.id}','是否要关闭该直播间？')" class="btn btn-sm bg-warning text-white">关闭直播</a>`;
              }
              return `
              <div class="actions btn-group btn-group-sm">
                  <a @click="openInfo('/admin/live/look/${item.id}','观看记录')" class="btn btn-sm bg-primary text-white">观看记录</a>                        
                  <a @click="openInfo('/admin/live/gift/${item.id}','礼物记录')" class="btn btn-sm bg-purple text-white">礼物记录</a>                 
                  <a @click="openInfo('/admin/live/comment/${item.id}','弹幕记录')" class="btn btn-sm bg-success text-white">弹幕记录</a>         
                  ${close}        
                  <a @click="del('/admin/live/delete/${item.id}')" class="btn btn-sm bg-danger text-white">删除</a> 
              </div>
              `;
            },
          },
        ],
      },
      data,
    });
  }
  //观看记录
  async look(){``
    const { ctx , app}  =this;
    let id = ctx.params.id;

    let res = await app.model.LiveUser.findAll({
      where:{
        live_id:id
      },
      include:[
        {
          model:app.model.User,
          attributes:['id','username']
        }
      ]
    })
    ctx.apiSuccess({
      ths:[
        {
          title: "用户名",
          key: "username"
        }, {
            title: "观看时间",
            key: "created_time"
        }
      ],
      data:res.map(item => {
        return {
          id:item.id,
          username:item.user.username,
          created_time:app.formatTime(item.created_time)
        }
      })
    })
  }
  //礼物记录
  async gift(){
    const { ctx , app}  =this;
    let id = ctx.params.id;

    let res = await app.model.LiveGift.findAll({
      where:{
        live_id:id
      },
      include:[
        {
          model:app.model.User,
          attributes:['id','username']
        },
        {
          model:app.model.Gift,
        }
      ]
    })
    ctx.apiSuccess({
      ths:[
        {
          title: "礼物名称",
          key: "gift_name"
        }, {
            title: "礼物图标",
            key: "gift_image",
            type:"image"
        },
        {
            title: "礼物金币",
            key: "gift_coin"
        },
        {
            title: "赠送者",
            key: "username"
        },
         {
            title: "赠送时间",
            key: "created_time"
        },
      ],
      data:res.map(item => {
        return {
          created_time:app.formatTime(item.created_time),
          gift_name:item.gift.name,
          gift_coin:item.gift.coin,
          gift_image:item.gift.image,
          username:item.user.username,
          avatar:item.user.avatar,
        }
      })
    })
  }
  //评论记录
  async comment(){
    const { ctx , app}  =this;
    let id = ctx.params.id;

    let res = await app.model.Comment.findAll({
      where:{
        live_id:id
      },
      include:[
        {
          model:app.model.User,
          attributes:['id','username']
        }
      ]
    })
    ctx.apiSuccess({
      ths:[
          {
            title: '内容',
            key: 'content',
        },{
            title: '发送人',
            key: 'username',
        },{
            title: '发送时间',
            key: 'created_time',
        }
      ],
      data:res.map(item => {
        return {
          content:item.content,
          created_time:app.formatTime(item.created_time),
          username:item.user.username,
          avatar:item.user.avatar,
        }
      })
    })
  }
  //关闭直播间
  async close(){
    const { ctx , app}  =this;
    let id = ctx.params.id;

    let live = await app.model.Live.findOne({
      where:{
        id
      }
    })

    if(!live){
      ctx.toast('该直播间不存在','danger');
    } else if(live.status === 3){
      ctx.toast('该直播间已经结束','danger');
    } else {
      live.status = 3;
      await live.save();
      ctx.toast('操作成功','success');
    }

    ctx.redirect('/admin/live');
  }

   //删除直播间
   async delete() {
    const { ctx, app } = this;
    const id = ctx.params.id;
    await app.model.Live.destroy({
      where: {
        id,
      },
    });

    ctx.toast("删除成功", "success");
    ctx.redirect("/admin/live");
  }
}

module.exports = LiveController;
